import { defineStore } from 'pinia'
import { login } from "../api/login.js"
import jsCookie from "js-cookie"
export const useLoginStore = defineStore('login', {
    state: () => ({
        test: "测试",
        remberMeFlag: false,
        remberObj: { username: '', password: '' },
        userInfo: { username: "", password: "", token: '' }
    }),
    getters: {
        token:()=>{
            return this.userInfo.token
        }
    },
    actions: {
        setRember(flag, username = "", password = "") {
            this.remberMeFlag = flag
            if (flag) {
                this.remberObj.username = username
                this.remberObj.password = password
            } else {
                this.remberObj = { username: '', password: '' }
            }
        },
        doLogin(username, password) {
            return new Promise((resolve, reject) => {
                login(username, password).then(res => {
                    this.userInfo = res
                    jsCookie.set("token", res.token, { expires: 7 })
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })

        }
    },
})